<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class UserCitiesController extends AppController {

    var $name = 'UserCities';

    function index($user_id, $branch) {
        App::Import('model', 'User');
        $User = new User();

        $tipo = $User->field('branch_id', array('User.branch_id' => $user_id));
        $this->set('grupo',$this->Auth->user('group_id') );
        $this->UserCity->recursive = -1;
        $this->set('user_id', $user_id);
        $this->set('branch', $branch);
        $this->set('cities', $this->UserCity->find('all', array('conditions' => array('UserCity.user_id' => $user_id), 'fields' => array('UserCity.id', 'User.group_id', 'City.nombre', 'Departament.nombre'), 'joins' => array(array('table' => 'users', 'alias' => 'User', 'type' => 'left', 'conditions' => array('User.id=UserCity.user_id')), array('table' => 'cities', 'alias' => 'City', 'type' => 'left', 'conditions' => array('City.id=UserCity.city_id')), array('table' => 'departaments', 'alias' => 'Departament', 'type' => 'left', 'conditions' => array('Departament.id=City.departament_id'))))));
    }

    function add($user_id, $branch) {
        $this->set('user_id', $user_id);
        $this->set('branch', $branch);
        if (empty($this->data)) {
            App::Import('model', 'Departament');
            $Departament = new Departament();
            $this->set('departaments', $Departament->find('list', array('fields' => array('Departament.id', 'Departament.nombre'))));
        } else {
            if ($this->UserCity->saveAll($this->data)) {
                $this->Session->setFlash('Municipio agregado con éxito', 'flash_custom');
                $this->redirect(array('controller' => 'UserCities', 'action' => 'index', $user_id, $branch));
            }
        }
    }

    function select() {
        App::Import('model', 'Departament');
        $Departament = new Departament();
        $this->set('cities', $Departament->City->find('list', array('fields' => array('City.id', 'City.nombre'), 'conditions' => array('City.departament_id' => $this->data['UserCity']['departament_id']))));
    }

    function delete($id, $user_id, $branch) {
        if ($this->UserCity->delete($id)) {
            $this->Session->setFlash('Municipio eliminado con éxito.', 'flash_custom');
            $this->redirect(array('controller' => 'UserCities', 'action' => 'index', $user_id, $branch));
        }
    }

}

?>
